<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class groups_to_object extends CI_Controller {

        function __construct()
        {
            parent::__construct();
            $this->load->model('groups_to_object_model','gtb_model');
        }

        public function index(){show_404();}

        public function set_default()
        {
            $type = $_POST['object_type'];
            $custom = 0;

            $groups_length = count($_POST['groups']);

            // DELETE OBJECT GROUP FIRST
            $q = "DELETE FROM object_groups WHERE object_type = '$type'";
            $this->db->query($q);

            for($i=0;$i<$groups_length;$i++){
                $data = array(
                    'object_type'=>$type,
                    'custom'=>$custom,
                    'groups_ID'=>$_POST['groups'][$i],
                );
                //$this->firephp->log($data);
                $this->db->insert('object_groups',$data);
            }

            redirect(base_url("groups_to_object/view_all?object_type=$type"));
        }

        function view_all()
        {
            $this->load->model('groups_model');

            $this->data = array(
                'title' => "View groups",
                'groups_to_object' => $this->gtb_model->get_object_groups($_GET['object_type'],TRUE),
                'groups' => $this->groups_model->view_all_groups()
            );
            $this->firephp->log($this->data);
            $this->template->build('groups_to_object/view_all_groups',$this->data);
        }
}